﻿package com.codingbros.header.graphics 
{
	
	/**
	 * ...
	 * @author peter@codingbros.com
	 */
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point
	import flash.display.Bitmap;
	import flash.display.BitmapData;

	
	public class GraphicsWobblyLines extends Sprite 
	{	
		private static const WIDTH:int = 1000;
		private static const HEIGHT:int = 200;
		
		private static const COLORS:Array = new Array(0xB2ED45, 0x3E8F66, 0x084557, 0x381529, 0xEA3732);
		
		private var _particles:Array;
		private static const PARTICLESNUM:int = 200;
		
		private var _particlesCanvas:Shape;
		
		private static const ADDPARTICLESTIME:int = WIDTH / PARTICLESNUM;
		private var _addParticlesCountDown:int = ADDPARTICLESTIME;	
		
		
		private var _offsets:				Array		= [];
		private var _offsetSpeed1:			Number		= 2.0;
		private var _offsetSpeed2:			Number		= -2.0;
		private var _baseX:					int			= 180;
		private var _baseY:					int			= 180;
		private var _noiseSeed:				int			= 1000;
		private var _noiseBMD:				BitmapData;
		private var _noiseBMP:				Bitmap;
		
		
		
		public function GraphicsWobblyLines () 
		{
			_noiseBMD = new BitmapData( WIDTH, HEIGHT );
			_noiseBMP = new Bitmap( _noiseBMD );
			_noiseBMP.visible = false;
			addChild( _noiseBMP );		
			
			_offsets.push( new Point());
			_offsets.push( new Point());			
			
			_particlesCanvas = new Shape();
			this.addChild(_particlesCanvas);
			
			createParticles();
			
			this.addEventListener(Event.ENTER_FRAME, update);
		}
		
		private function createParticles():void 
		{
			_particles = new Array();
			
			var p:Particle
			for (var i:int = 0; i < PARTICLESNUM; i ++) 
			{
				p = new Particle();
				
				p._color = COLORS[Math.floor(i/PARTICLESNUM * COLORS.length)];
				
				p._point1X = WIDTH / PARTICLESNUM * i;
				p._point1Y = 0;
				p._point1Vel = 1.0;
				p._point1Acc = 0;
				p._point1Rotation = Math.PI;
				
				p._point2X = WIDTH / PARTICLESNUM * i;
				p._point2Y = 0;
				p._point2Vel = 1.0;
				p._point2Acc = 0;
				p._point2Rotation = Math.PI;
				
				_particles.push(p);
			}
		}
		
		
		private function update(e:Event):void
		{				
			_offsets[0].x += _offsetSpeed1;
			_offsets[0].y += _offsetSpeed1;
			
			_offsets[1].x += _offsetSpeed2;
			_offsets[1].y += _offsetSpeed2;
			
			_noiseBMD.perlinNoise( _baseX, _baseY, 2, _noiseSeed, false, true, 10, true, _offsets );
			
			var p:Particle;
			
			var brightness1:Number;
			var pixel1:int;	
			
			var brightness2:Number;
			var pixel2:int;
			
			var widthRatio:Number;
			
			_particlesCanvas.graphics.clear();
			
			for (var i:int = 0; i < _particles.length; i++) 
			{
				p = _particles[i] as Particle;
				
				pixel1 = _noiseBMD.getPixel( p._point1X, p._point1Y);
				pixel2 = _noiseBMD.getPixel( p._point2X, p._point2Y);
				
				brightness1 = pixel1 / 0xFFFFFF;
				brightness2 = pixel2 / 0xFFFFFF;
				
				widthRatio = p._point1X / WIDTH;
				
				//p.update();
				
				_particlesCanvas.graphics.lineStyle(1, p._color);
				_particlesCanvas.graphics.moveTo(p._point1X, p._point1Y);
				_particlesCanvas.graphics.lineTo(p._point2X, p._point2Y + (brightness2 * HEIGHT / 3));
			}
		}		
	}	
}


import flash.geom.Point;

internal class Particle
{	
	public var _color:uint;
	
	public var _point1X:Number;
	public var _point1Y:Number;
	public var _point1Vel:Number;
	public var _point1Acc:Number;
	public var _point1Rotation:Number;
	
	public var _point2X:Number;
	public var _point2Y:Number;
	public var _point2Vel:Number;
	public var _point2Acc:Number;
	public var _point2Rotation:Number;
	
	public function Particle()
	{		
	}
	
	public function update():void 
	{
		_point1Vel += _point1Acc;
		
		_point1X += Math.cos( _point1Rotation ) * _point1Vel;
		_point1Y += Math.sin( _point1Rotation ) * _point1Vel;
		
		_point2Vel += _point2Acc;
		
		_point2X += Math.cos( _point2Rotation ) * _point2Vel;
		_point2Y += Math.sin( _point2Rotation ) * _point2Vel;
	}	
}